package cn.itsource.pethome.org.controller;

import cn.itsource.pethome.basic.util.RedisUtils;
import cn.itsource.pethome.org.domain.Employee;
import cn.itsource.pethome.org.query.EmployeeQuery;
import cn.itsource.pethome.org.service.IEmployeeService;
import cn.itsource.pethome.basic.util.AjaxResult;
import cn.itsource.pethome.basic.util.PageList;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private IEmployeeService employeeService;

    /**
     * 员工添加方法
     * @param employee
     * @return
     */
    //@RequestMapping(value = "/save",method = RequestMethod.PUT)
    //RequestBody接收一个接送格式对象
    @PutMapping()
    public AjaxResult save(@RequestBody Employee employee){
        try {
            employeeService.save(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"添加失败" +e.getMessage());
        }
    }

    /**
     * 员工修改
     * @param employee
     * @return
     */
    //RequestMapping(value = "/update",method = RequestMethod.POST)
    @PostMapping
    public AjaxResult update(@RequestBody Employee employee){
        try {
            employeeService.update(employee);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"修改失败" +e.getMessage());
        }
    }
    /**
     * 员工删除
     * @param id
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            employeeService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败" +e.getMessage());
        }
    }
    /**
     * 员工批量删除
     * @param ids
     * @return
     */
    //@RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    @PatchMapping("/del")//批量删除用数组接收参数
    public AjaxResult delete(@RequestBody Long[] ids){
        try {
            //调用批量删除方法
            employeeService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败!网络繁忙！" +e.getMessage());
        }
    }

    /**
     * 根据条件查询所有员工
     * @return
     */
    @PatchMapping
    public PageList<Employee> loadPageByQuery(@RequestBody EmployeeQuery query){

        return employeeService.loadPageByQuery(query);
    }

    /**
     *  根据id查询员工
     * @param id
     * @return
     */
    //@RequestMapping(value = "/loadById",method = RequestMethod.GET)
    @GetMapping("{id}")
    public Employee loadById(@PathVariable("id") Long id){
        return employeeService.loadById(id);
    }


    /**
     * 后台用户登录
     * @param employee
     * @return
     */
    //RequestMapping(value = "/update",method = RequestMethod.POST)
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Employee employee){
        try {
            Employee dbEmployee = employeeService.login(employee);
            //将登录对象转换为json字符串
            String userJsonStr = JSON.toJSONString(dbEmployee);
            //获取一个token 令牌
            String token = UUID.randomUUID().toString();
            //将登录对象存储到redis中
            RedisUtils.INSTANCE.set(token, userJsonStr, 60 * 30);
            //创建map集合存储登录用户和token
            Map<String, Object> map = new HashMap<>();
            map.put("user", dbEmployee);
            map.put("token", token);
            return new AjaxResult().setResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        }
    }


}
